<?php 
namespace app\admin\controller;

use app\common\model\shop\ShopSkillTime;
use think\Db;
use think\Exception;
use app\common\controller\Log;
use app\common\model\shop\ShopSkillGoods;
use app\common\model\shop\ShopGoods;

class Skill extends Base
{
	/**
	 * 添加时间段
	 * @return unknown
	 */
	public function addSkillTime()
	{
		$data = $this->request->post('');
		Db::startTrans();
		try {
			$data['create_time'] = time();
			ShopSkillTime::create($data);
			//添加日志
			Log::addLog(1, '添加秒杀时间段【'.$data['start_time'].'-'.$data['end_time'].'】');
			Db::commit();
		} catch (Exception $e) {
			Db::rollback();
			return jsonError($e->getMessage());
		}
		return jsonSuccess('添加成功');
	}
	
	/**
	 * 获取时间段
	 * @return unknown
	 */
	public function getSkillTimes()
	{
		$list = ShopSkillTime::where(['state' => 1])->order('start_time asc')->select();
		return jsonSuccess($list);
	}
	
	/**
	 * 删除秒杀时间段
	 * @return unknown
	 */
	public function deleteSkillTimes()
	{
		$id = $this->request->post('id');
		Db::startTrans();
		try {
			$skil_times = ShopSkillTime::get($id);
			$skil_times->save(['state' => 0]);
			//清除该时间段的所有商品
			ShopSkillGoods::where(['time_id' => $id])->update(['update_time' => time(),'state' => 0]);
			//添加日志
			Log::addLog(3, "删除时间段【{$skil_times->start_time} - {$skil_times->end_time}】");
			Db::commit();
		} catch (Exception $e) {
			Db::rollback();
			return jsonError($e->getMessage());
		}
		return jsonSuccess('删除成功');
	}
	
	/**
	 * 添加秒杀产品
	 * @throws Exception
	 * @return unknown
	 */
	public function addSkillGoods()
	{
		$data = $this->request->post('');
		Db::startTrans();
		try {
			$info = [];
			if(!$data['goods_id'])throw new Exception('请选择商品');
			if(!count($data['time_id']))throw new Exception('请选择时间段');
			if(!$data['price'])throw new Exception('请输入价格');
			if(!$data['num'])throw new Exception('请输入数量');
			foreach ($data['time_id'] as $rows) {
				$temp = [
						'goods_id' => $data['goods_id'],
						'time_id' => $rows,
						'num' => $data['num'],
						'price' => $data['price'],
						'create_time' => time(),
						'update_time' => time(),
						'state' => 1,
						'surplus_num' => $data['num']
				];
				$info[] = $temp;
			}
			$skil_goods = new ShopSkillGoods();
			$skil_goods->saveAll($info);
			Db::commit();
		} catch (Exception $e) {
			Db::rollback();
			return jsonError($e->getMessage());
		}
		return jsonSuccess('添加成功');
	}
	
	/**
	 * 秒杀商品列表
	 * @return unknown
	 */
	public function skillGoodsList()
	{
		$time_id = $this->request->post('time_id');
		$where = ['state' => 1];
		if($time_id)$where['time_id'] = $time_id;
		$list = ShopSkillGoods::where($where)->order('id desc')->paginate()->toArray();
		foreach ($list['data'] as &$rows) {
			$rows['times'] = ShopSkillTime::get($rows['time_id']);
			$rows['goods_name'] = ShopGoods::where(['id' => $rows['goods_id']])->value('goods_name');
		}
		return jsonSuccess($list);
 	}
 	
 	/**
 	 * 删除秒杀商品
 	 * @return unknown
 	 */
 	public function deleteSkillGoods()
 	{
 		$id = $this->request->post('id');
 		try {
 			$skill_goods = ShopSkillGoods::get($id);
 			$save = ['state' => 0 , 'update_time' => time()];
 			$skill_goods->save($save);
 		} catch (Exception $e) {
 			return jsonError($e->getMessage());
 		}
 		return jsonSuccess('删除成功');
 	}
}